<html>
<head><meta charset="utf-8"><title>stacked borrows: change tag on deref or cast? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked.20borrows.3A.20change.20tag.20on.20deref.20or.20cast.3F.html">stacked borrows: change tag on deref or cast?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="147849239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked%20borrows%3A%20change%20tag%20on%20deref%20or%20cast%3F/near/147849239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked.20borrows.3A.20change.20tag.20on.20deref.20or.20cast.3F.html#147849239">(Nov 16 2018 at 22:01)</a>:</h4>
<p>One slightly unsatisfying point in stacked borrows is that I have a rule that when you deref a raw pointer (<code>*x</code>), that deref forgets the tag -- so e.g. <code>&amp;*x</code> is not actually the identity on a raw pointer. I now realized I could get rid of this if <em>instead</em> I make it so that casts forget the tag. Now I am torn because I am not sure what I like more: cast being the same as transmute (in MIR; the surface-level <code>x as *mut _</code> also emits an <code>EscapeToRaw</code> and is very much not like a transmute), or deref not changing the tag (and hence <code>&amp;*</code> being the identity). I <em>think</em> the compiler optimizes <code>&amp;*</code> away, maybe even for raw ptr, and does not optimize casts away, which indicates erase-tag-on-cast might be better than erase-tag-on-raw-ptr-deref. Any thoughts?</p>



<a name="147869118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked%20borrows%3A%20change%20tag%20on%20deref%20or%20cast%3F/near/147869118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/stacked.20borrows.3A.20change.20tag.20on.20deref.20or.20cast.3F.html#147869118">(Nov 17 2018 at 08:07)</a>:</h4>
<p>After sleeping about it, I think I prefer erase-tag-on-cast. That makes a raw ptr deref do absolutely nothing to the tag and the stack, which means we can faithfully say that ptr derefs in C are like raw ptr derefs in Rust.<br>
It also means that you better use a cast, not a transmute, to "export" your pointers. But that has pretty much already been true before this change (and I am aware it can be a problem for some patterns).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>